package com.sifeng.test;

import com.sifeng.utils.StringUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

/**
 * Created by yang_gsh on 2018/12/12.
 */
public class RedeemCode {
	public static String[] chars = new String[] { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z" };

	/**
	 *
	 * @param codecount  生成个数
	 * @param filePath   生成的文件路径
	 * @param actId      活动编号
	 * @throws IOException
	 */
	public static void create(int codecount, String filePath, String actId) throws IOException {
		ShowTime();
		StringBuilder content = new StringBuilder();
		int y = Integer.parseInt(actId.substring(0,6), 16);
		y += Integer.parseInt(actId.substring(7), 16);
		for(int i = 0 ; i < codecount ; i ++) {
			content.append(generateShortUuid(y));
			content.append("\n");
		}
		ShowTime();

		writeContent(filePath, content.toString());
		ShowTime();
	}

	public static void create(int codecount, String filePath, String actId, String key) throws Exception {
		ShowTime();
		StringBuilder contentXor = new StringBuilder();
		StringBuilder contentMd5 = new StringBuilder();
		//StringBuilder content = new StringBuilder();
		int y = Integer.parseInt(actId.substring(0,6), 16);
		y += Integer.parseInt(actId.substring(7), 16);
		for(int i = 0 ; i < codecount ; i ++) {
			String str = generateShortUuid(y);
			/*content.append(str);
			content.append("\n");*/
			String xorStr = XOREncrypt(str, key);
			contentXor.append(xorStr);
			contentXor.append("\n");
			contentMd5.append(EncoderByMd5(str) + "," + xorStr);
			contentMd5.append("\n");
		}
		ShowTime();

		writeContent(filePath, contentXor.toString());
		writeContent(filePath + ".md5", contentMd5.toString());
		//writeContent(filePath + ".tmp", content.toString());
		ShowTime();
	}

	public static String generateShortUuid(int seed) {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");

		shortBuffer.append(chars[seed % 36]);

		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 36]);
		}

		return shortBuffer.toString();

	}

	public static void writeContent(String filePath, String content) throws IOException {
		FileWriter writer = new FileWriter(filePath);
		writer.write(content);
		writer.flush();
		writer.close();
	}

	public static void ShowTime(){
		Date date = new Date();
		long times = date.getTime();//时间戳
		System.out.println("time  : " + times);
	}

	private static String XOREncrypt(String data, String key) {
		StringBuffer result = new StringBuffer();
		int j = 0;
		for (int i = 0; i < data.length(); i++) {
			if (j == key.length()) {
				j = 0;
			}
			result.append(StringUtils.leftPad(
					Integer.toHexString(data.charAt(i) ^ key.charAt(j)), 2, '0'));
			j++;
		}
		return result.toString();
	}

	private static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest md5 = MessageDigest.getInstance("MD5");

		char[] charArray = str.toCharArray();
		byte[] byteArray = new byte[charArray.length];
		for (int i = 0; i < charArray.length; i++) {
			byteArray[i] = (byte) charArray[i];
		}
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}

		return hexValue.toString().toUpperCase();
	}

	public static void main(String[] args) throws Exception {
		RedeemCode.create(100000,"E:\\test_20190102.dat", "1812120037","123456789");
		//System.out.println(System.currentTimeMillis());

		//System.out.println(Integer.parseInt("315408", 16));
		/*String tmp = "1544595069921";
		System.out.println(tmp);
		System.out.println(tmp.substring(0,6) +tmp.substring(6));*/
	}
}
